package com.syaccp.controller;

import com.syaccp.model.Customer;
import com.syaccp.model.CustomerCoupon;
import com.syaccp.model.SysUser;
import com.syaccp.request.CustomerAddAndUpdateRequest;
import com.syaccp.request.CustomerSelectRequest;
import com.syaccp.service.CustomerCouponService;
import com.syaccp.service.CustomerService;
import com.syaccp.utils.Result;
import com.syaccp.utils.SecurityUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Api(tags = "客户操作")
@RestController
@RequestMapping("/api/customer")
public class CustomerController {

    @Resource
    private CustomerService customerService;

    @Resource
    CustomerCouponService customerCouponService;


    @ApiOperation(value = "根据登录用户查询客户页面头部信息")
    @GetMapping("/findCustomerHeader")
    public Result findCustomerHeader(){
        SysUser sysUser= SecurityUtil.getLoginUser();
        return customerService.findCustomerHeader(sysUser);
    }

    @ApiOperation(value = "根据传递过来的条件进行分页查询")
    @PostMapping("/findCustomer")
    public Result findCustomer(@RequestBody CustomerSelectRequest customerSelectRequest){
        System.out.println("customerSelectRequest = " + customerSelectRequest);
        return customerService.findCustomerPage(customerSelectRequest);
    }



    @ApiOperation(value = "根据传递过来的id查询客户对象")
    @GetMapping("/findCustomerById/{id}")
    public Result findCustomerById(@PathVariable Integer id){

        return customerService.findCustomerById(id);
    }


    @ApiOperation(value = "修改客户信息")
    @PutMapping("/updateCustomer")
    public Result updateCustomer(@RequestBody @Validated CustomerAddAndUpdateRequest caaur){

        return customerService.updateCustomer(caaur);
    }


    @ApiOperation(value = "新增客户信息")
    @PostMapping("/addCustomer")
    public Result addCustomer(@RequestBody @Validated CustomerAddAndUpdateRequest caaur){
        SysUser loginUser = SecurityUtil.getLoginUser();
        return customerService.addCustomer(caaur,loginUser);
    }

    @ApiOperation(value = "获取制作公司列表")
    @RequestMapping(value = "/getCustomerInfo", method = RequestMethod.GET)
    public Result<List<Customer>> queryCustomerInfo() {
        return Result.success(customerService.queryCustomerInfo());
    }

    @ApiOperation(value = "获取客户优惠券")
    @ApiImplicitParam(name = "userId", value = "用户ID")
    @RequestMapping(value = "/getCustomerCoupon/{userId}", method = RequestMethod.GET)
    public Result<List<CustomerCoupon>> queryCustomerCoupon(@PathVariable Integer userId) {
        return Result.success(customerCouponService.queryCustomerCoupon(userId));
    }

    @ApiOperation(value = "使用优惠券")
    @ApiImplicitParam(name = "id", value = "优惠券ID")
    @RequestMapping(value = "/useCustomerCoupon/{couponId}/{orderId}", method = RequestMethod.PUT)
    public Result<Integer> useCustomerCoupon(@PathVariable Integer couponId,
                                             @PathVariable Integer orderId) {
        return Result.success(customerCouponService.useCustomerCoupon(couponId, orderId));
    }
}
